<template>
  <div class="app-container">
    <el-table v-loading="loading" :data="list">
      <el-table-column label="编号" align="center" sortable prop="id" width="120" />
      <el-table-column label="功能" align="center" prop="name" />
<!--      <el-table-column label="图标" align="center" prop="icon">-->
<!--        <template slot-scope="scope">-->
<!--          <el-image :src="scope.row.icon"></el-image>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="简介" align="center" prop="intro" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
          >编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 编辑对话框 -->
    <el-dialog title="编辑" :visible.sync="open" width="50%" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="功能：" prop="name">
          <el-input v-model="form.name" placeholder="请输入功能" />
        </el-form-item>
        <el-form-item label="简介：" prop="intro">
          <el-input v-model="form.intro" placeholder="请输入简介" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">保 存</el-button>
        <el-button @click="cancel">返 回</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getFunctionList, saveFunctionInfo } from "@/api/shuiyin/shuiyin";

export default {
  name: "Function",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 功能管理表格数据
      list: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "渠道名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询功能管理列表 */
    getList() {
      this.loading = true;
      getFunctionList(this.queryParams).then(response => {
        console.log(response.rows)
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    // 表单重置
    reset() {
      this.form = {
        name: null,
        intro: null,
      };
      this.resetForm("form");
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      this.form = JSON.parse(JSON.stringify(row));
      this.open = true;
      this.title = "修改";
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            saveFunctionInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
  }
};
</script>
